// 3. 数字序列模块 - 管理1-9数字随机序列
module digit_sequence(
    input clk,
    input rst_n,
    input game_start,
    input game_reset,
    input digit_advance, // 前进到下一个数字
    output reg [3:0] current_digit,     // 当前显示数字(1-9)
    output reg [3:0] digit_index,       // 当前数字索引(0-8)
    output reg sequence_complete
);

    // 线性反馈移位寄存器用于生成伪随机数
    reg [15:0] lfsr;
    
    // 预定义的9种不同的随机序列，根据LFSR的不同位选择
    reg [3:0] sequence_table [8:0] [8:0];
    
    // 当前使用的序列索引
    reg [3:0] sequence_select;
    
    // 初始化序列表
    initial begin
        // 序列0: 基于一种随机排列
        sequence_table[0][0] = 4'd3; sequence_table[0][1] = 4'd7; sequence_table[0][2] = 4'd1;
        sequence_table[0][3] = 4'd9; sequence_table[0][4] = 4'd5; sequence_table[0][5] = 4'd2;
        sequence_table[0][6] = 4'd8; sequence_table[0][7] = 4'd4; sequence_table[0][8] = 4'd6;
        
        // 序列1
        sequence_table[1][0] = 4'd5; sequence_table[1][1] = 4'd2; sequence_table[1][2] = 4'd8;
        sequence_table[1][3] = 4'd1; sequence_table[1][4] = 4'd6; sequence_table[1][5] = 4'd9;
        sequence_table[1][6] = 4'd3; sequence_table[1][7] = 4'd7; sequence_table[1][8] = 4'd4;
        
        // 序列2
        sequence_table[2][0] = 4'd9; sequence_table[2][1] = 4'd4; sequence_table[2][2] = 4'd2;
        sequence_table[2][3] = 4'd6; sequence_table[2][4] = 4'd1; sequence_table[2][5] = 4'd8;
        sequence_table[2][6] = 4'd5; sequence_table[2][7] = 4'd3; sequence_table[2][8] = 4'd7;
        
        // 序列3
        sequence_table[3][0] = 4'd2; sequence_table[3][1] = 4'd6; sequence_table[3][2] = 4'd9;
        sequence_table[3][3] = 4'd3; sequence_table[3][4] = 4'd7; sequence_table[3][5] = 4'd1;
        sequence_table[3][6] = 4'd4; sequence_table[3][7] = 4'd8; sequence_table[3][8] = 4'd5;
        
        // 序列4
        sequence_table[4][0] = 4'd7; sequence_table[4][1] = 4'd1; sequence_table[4][2] = 4'd4;
        sequence_table[4][3] = 4'd8; sequence_table[4][4] = 4'd3; sequence_table[4][5] = 4'd6;
        sequence_table[4][6] = 4'd9; sequence_table[4][7] = 4'd5; sequence_table[4][8] = 4'd2;
        
        // 序列5
        sequence_table[5][0] = 4'd1; sequence_table[5][1] = 4'd8; sequence_table[5][2] = 4'd5;
        sequence_table[5][3] = 4'd2; sequence_table[5][4] = 4'd9; sequence_table[5][5] = 4'd4;
        sequence_table[5][6] = 4'd7; sequence_table[5][7] = 4'd6; sequence_table[5][8] = 4'd3;
        
        // 序列6
        sequence_table[6][0] = 4'd4; sequence_table[6][1] = 4'd9; sequence_table[6][2] = 4'd7;
        sequence_table[6][3] = 4'd5; sequence_table[6][4] = 4'd2; sequence_table[6][5] = 4'd3;
        sequence_table[6][6] = 4'd1; sequence_table[6][7] = 4'd6; sequence_table[6][8] = 4'd8;
        
        // 序列7
        sequence_table[7][0] = 4'd6; sequence_table[7][1] = 4'd3; sequence_table[7][2] = 4'd1;
        sequence_table[7][3] = 4'd7; sequence_table[7][4] = 4'd4; sequence_table[7][5] = 4'd5;
        sequence_table[7][6] = 4'd2; sequence_table[7][7] = 4'd9; sequence_table[7][8] = 4'd8;
        
        // 序列8
        sequence_table[8][0] = 4'd8; sequence_table[8][1] = 4'd5; sequence_table[8][2] = 4'd3;
        sequence_table[8][3] = 4'd4; sequence_table[8][4] = 4'd6; sequence_table[8][5] = 4'd7;
        sequence_table[8][6] = 4'd2; sequence_table[8][7] = 4'd1; sequence_table[8][8] = 4'd9;
    end
    
    // LFSR生成伪随机数 (多项式: x^16 + x^14 + x^13 + x^11 + 1)
    always @(posedge clk or negedge rst_n) begin
        if (!rst_n) begin
            lfsr <= 16'hACE1; // 初始种子值（非零）
        end else begin
            lfsr <= {lfsr[14:0], lfsr[15] ^ lfsr[13] ^ lfsr[12] ^ lfsr[10]};
        end
    end
    
    // 主控制逻辑
    always @(posedge clk or negedge rst_n) 
    begin
        if (!rst_n) 
        begin
            current_digit <= 4'd1;
            digit_index <= 4'd0;
            sequence_complete <= 1'b0;
            sequence_select <= 4'd0;
        end 
        else if (game_reset || game_start) 
        begin
            // 根据当前LFSR值选择一个序列
            sequence_select <= lfsr[3:0] % 9;  
            current_digit <= sequence_table[lfsr[3:0] % 9][0]; // 选择序列的第一个数字
            digit_index <= 4'd0;
            sequence_complete <= 1'b0;
        end 
        else if (digit_advance && !sequence_complete) 
        begin
            if (digit_index == 4'd8) 
            begin
                sequence_complete <= 1'b1;
            end 
            else 
            begin
                digit_index <= digit_index + 1'b1;
                current_digit <= sequence_table[sequence_select][digit_index + 1'b1];
            end
        end
    end

endmodule